package com.xiaodou.mjf.application.content.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.xiaodou.mjf.application.content.repository.BannerRepository;
import com.xiaodou.mjf.application.content.service.BannerService;
import com.xiaodou.mjf.entity.content.Banner;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;

/***
 *
 * Banner业务访问实现
 * 
 * @author XiaoDou.com Code Generator
 * @version 1.0 2014-10-29 17:37:52
 *
 */
@Service
@Transactional
public class BannerServiceImpl extends JpaSimpleService<Banner, String>
		implements BannerService {

	Logger logger = LoggerFactory.getLogger(BannerServiceImpl.class);

	@Resource
	BannerRepository bannerRepository;

	@Override
	protected JpaRepository<Banner, String> getRepository() {
		return bannerRepository;
	}

	@Override
	public List<Banner> list(Banner banner) {

		List<Banner> banners = bannerRepository.findByTypeAndStatus(banner.getType(), banner.getStatus());

		return banners;
	}

	@Override
	public Page<Banner> pageAll(Banner entity) {
		return bannerRepository.findAll(
				(root, query, cb) -> {
					Predicate p1 = cb.equal(root.get("status")
							.as(Integer.class), entity.getStatus());
					Predicate p2 = cb.like(root.get("title").as(String.class),
							"%" + entity.getTitle() + "%");
					
					if(entity.getStatus().equals(-1)){
			        	  return query.where(p2).getRestriction();     
			            }
					return query.where(p1, p2).getRestriction();
				},
				preparePage(entity, new Sort(new Sort.Order(Sort.Direction.ASC,
						"sort"))));
	}
}
